/**
 * @(#) WelfareCodesProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	선택적복리 후생 리스트 Proxy - Proxy
 * 				선택적복리 후생 리스트 년도별 조회
 */
package com.skt.common_hr.welfare.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.welfare.WelfareFacade;
	import com.skt.common_hr.welfare.model.vo.CodeVO;
	import com.wcl.dataloader.*;
	
	import mx.collections.ArrayCollection;
	import mx.resources.ResourceManager;
		
	public class WelfareCodesProxy extends CommonProxy
	{
		public static const NAME:String = "WelfareCodesProxy";
		
		public var usedClassList:ArrayCollection = new ArrayCollection();
		public var usedItemList:ArrayCollection = new ArrayCollection();
		public var patronInfo:ArrayCollection = new ArrayCollection();
		public var tradeInfo:ArrayCollection = new ArrayCollection();
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function WelfareCodesProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @return void
		 */
		public function load():void
		{
			var param:Object = new Object();
			loadMultiData("Z_HR_0056", param, resultUsedClass);
			loadMultiData("Z_HR_0057", param, resultUsedItem);
		}
		
		//거래결재정보 콤보박스 데이터
		public function tradeComboLoad():void
		{
			 /*
			 (09.05.06.) 펑션  88 -> 64번으로 변경
			 var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0088");
			 */
			var param:Object = new Object();
			loadMultiData("Z_HR_0064", param, tradeComboResult);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultUsedClass(receiver:DataReceiver):void
		{
			if(receiver.retCode==0) {
				usedClassList = parseCodeList(receiver.result.OTAB);
				facade.sendNotification(WelfareFacade.LOAD_CODE_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_CODE_LIST_FAILED, this);
			}
		}
		
		public function resultUsedItem(receiver:DataReceiver):void
		{
			if(receiver.retCode==0) {  
				usedItemList = parseSubCodeList(receiver.result.OTAB);
				facade.sendNotification(WelfareFacade.LOAD_CODE_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_CODE_LIST_FAILED, this);
			}
		}
		
		
		//거래처정보, 결재정보 콤보박스 조회 성공
		public function tradeComboResult(receiver:DataReceiver):void
		{
			if(receiver.retCode==0) {
				patronInfo = parsePatronCodeList(receiver.result.T_STGBN);
				tradeInfo = parseTradeCodeList(receiver.result.T_APGBN);
				facade.sendNotification(WelfareFacade.LOAD_TRADECODE_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_TRADECODE_LIST_FAILED, this);
			}
		}
		

		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 초기화면 list
		 */
		private function parseCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection;
			var dummy:CodeVO = new CodeVO();
			dummy.gubun = "";
			dummy.CODE = "";
			dummy.TEXT = ResourceManager.getInstance().getString("Welfare", "SELECT_TYPE"); //"유형 선택";
			retArr.addItem(dummy);
			 for each(var rec:Object in srcArr) 
			{
				var vo:CodeVO = new CodeVO();
				vo.CODE = rec["CODE"];
				vo.TEXT = rec["TEXT"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 초기화면 list
		 */
		private function parseSubCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection;
			 for each(var rec:Object in srcArr) 
			{
				var vo:CodeVO = new CodeVO();
				vo.gubun = rec["CAFGBN"];
				vo.CODE = rec["CAFTY"];
				vo.TEXT = rec["CAFTX"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		//거래처 정보 콤보데이터
		private function parsePatronCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection;
			 for each(var rec:Object in srcArr) 
			{
				var vo:CodeVO = new CodeVO();
				vo.CODE = rec["CODE"];
				vo.TEXT = rec["TEXT"];
				retArr.addItem(vo);
			}															//'거래처정보 선택'
			retArr.addItemAt({CODE:'', TEXT:ResourceManager.getInstance().getString("Welfare", "SELECT_CUSTOM")}, 0);
			return retArr;	
		}
		
		//거래결재정보 콤보데이터
		private function parseTradeCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection;
			 for each(var rec:Object in srcArr) 
			{
				var vo:CodeVO = new CodeVO();
				vo.CODE = rec["CODE"];
				vo.TEXT = rec["TEXT"];
				retArr.addItem(vo);
			}//'결재정보 선택' 
			retArr.addItemAt({CODE:'', TEXT:ResourceManager.getInstance().getString("Welfare", "SELECT_ACCOUNT")}, 0);
			return retArr;	
		}
	}
}